//
//  RegisterVC.m
//  SH_JSK
//
//  Created by duwl on 1/27/15.
//  Copyright (c) 2015 asiainfo. All rights reserved.
//

#import "RegisterViewController.h"
#import "CommonMacro.h"
#import "UIImage+LXX.h"
#import "RegisterNextViewController.h"
#import "MBProgressHUD.h"
#import "JKAlert.h"


@interface RegisterViewController ()
{
    UIButton *checkBtn;
//    NSString *checkTure;
}
@property(nonatomic,strong)UITextField *phoneNum;
@property(nonatomic,strong)UITextField *checkNum;

@end

@implementation RegisterViewController


- (void)viewDidLoad
{
    [super viewDidLoad];
     [self.navigationController.navigationBar setBarTintColor:UIColorWithRGBA(89,214,158,1)];
    //左边按钮
    UIBarButtonItem *laft = [[UIBarButtonItem alloc]
                             initWithTitle:@"登陆"
                             style:UIBarButtonItemStylePlain
                             target:self
                             action:@selector(baby)];

    self.navigationItem.rightBarButtonItem = laft;
    self.title = @"注册";//设置navigationbar上显示的标题
    
    if (IS_OS_7_OR_LATER) {
        self.edgesForExtendedLayout = UIRectEdgeNone;
    }
}


-(void)baby{
    [self dismissViewControllerAnimated:YES completion:nil];
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    if (self.navigationController) {
        self.navigationController.navigationBarHidden = NO;
    }
}

- (void)loadView
{
    self.view = [[UIView alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    self.view.backgroundColor = [UIColor whiteColor];
    [self layout];
}

-(void)layout
{
    UIView *bgView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT)];
    [self.view addSubview:bgView];
    
    UIView *registView = [[UIView alloc]initWithFrame:CGRectMake(20,150, SCREEN_WIDTH-40, 110)];
    [bgView addSubview:registView];
    registView.layer.cornerRadius = 8;
    registView.layer.borderColor = UIColorWithRGBA(232, 234, 237, 1).CGColor;
    registView.layer.borderWidth = 1;
    
    
    UIView *line = [[UIView alloc]initWithFrame:CGRectMake(0, 60, SCREEN_WIDTH-40, 1)];
    line.backgroundColor = UIColorWithRGBA(232, 234, 237, 1);
    
    UIImageView *phoneImg = [[UIImageView alloc]initWithFrame:CGRectMake(20,25, 12,20)];
    phoneImg.image = [UIImage imageNamed:@"_0002s_0005_手机@2x"];
    [registView addSubview:phoneImg];
    
    UIImageView *checkImg = [[UIImageView alloc]initWithFrame:CGRectMake(20,75,15,20)];
    checkImg.image = [UIImage imageNamed:@"_0001s_0000s_0002_验证码@2x"];
    [registView addSubview:checkImg];
    
    
    _phoneNum = [[UITextField alloc]initWithFrame:CGRectMake(60, 20, SCREEN_WIDTH-40, 35)];
    _phoneNum.placeholder = @"请输入手机号码";
    
    _checkNum = [[UITextField alloc]initWithFrame:CGRectMake(60, 70, 200, 35)];
    _checkNum.placeholder = @"请输入验证码";
    [registView addSubview:_phoneNum];
    [registView addSubview:_checkNum];
    [registView addSubview:line];
    
    UIButton *nextTepBtn = [[UIButton alloc]initWithFrame:CGRectMake(20, 300, SCREEN_WIDTH-40, 50)];
    //    UIImage* image = [UIImage resizedImage:@"bg_green_button.9"];
    nextTepBtn.layer.cornerRadius = 8;
    nextTepBtn.backgroundColor = UIColorWithRGBA(89,214,158,1);
    [nextTepBtn setTitle:@"下一步" forState:UIControlStateNormal];
    [nextTepBtn addTarget:self action:@selector(nextTep) forControlEvents:UIControlEventTouchUpInside];
    [bgView addSubview:nextTepBtn];
    
    
    checkBtn = [[UIButton alloc]initWithFrame:CGRectMake(SCREEN_WIDTH-140, 70, 90, 35)];
    //   [checkBtn setBackgroundImage:image forState:UIControlStateNormal];
    [checkBtn setTitle:@"获取验证码" forState:UIControlStateNormal];
    [checkBtn addTarget:self action:@selector(checkClicked) forControlEvents:UIControlEventTouchUpInside];
    checkBtn.layer.cornerRadius = 8;
    checkBtn.titleLabel.font = [UIFont systemFontOfSize:12];
    checkBtn.backgroundColor = UIColorWithRGBA(89,214,158,1);
    [checkBtn setTintColor:[UIColor blackColor]];
    [checkBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [registView addSubview:checkBtn];
}


-(void)nextTep
{
     NSUserDefaults *userDefaultes = [NSUserDefaults standardUserDefaults];
    NSString *myString = [userDefaultes stringForKey:@"mycheck"];
    //此处发送请求
    NSLog(@"checNum为%@",myString);
    if(_phoneNum.text.length!=11) {
        
        [self showSimpleAlertView:@"请输入11位手机号码！"];
    }


 else  if ([_checkNum.text isEqualToString:myString]) {
     
     
     NSDictionary *param = @{@"phoneNumber": _checkNum};
    
     [UserModel getCheckNum:param withBlock:^(NSString *checkNum, NSError *error) {
         if (error) {
              [JKAlert showMessage:@"提示" message:@"网络请求失败"];
         }
         _checkTure = checkNum;
         [[NSUserDefaults standardUserDefaults] synchronize];
         RegisterNextViewController *regist = [[RegisterNextViewController alloc]init];
         regist.phoneNum = _phoneNum.text;
         regist.checkCode = _checkNum.text;
         [self.navigationController pushViewController:regist animated:YES];
     }];
   }
    
 else if ([_checkNum.text isEqualToString:_checkTure]==FALSE) {
     [self showSimpleAlertView:@"验证码输入有误！"];
 }
}

-(void)checkClicked
{
    if(_phoneNum.text.length!=11) {
        
        [self showSimpleAlertView:@"请输入11位手机号码！"];
    }
    else{
        //此处发送请求
        __block int timeout=180; //倒计时时间
        dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
        dispatch_source_t _timer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0,queue);
        dispatch_source_set_timer(_timer,dispatch_walltime(NULL, 0),1.0*NSEC_PER_SEC, 0); //每秒执行
        dispatch_source_set_event_handler(_timer, ^{
            if(timeout<=0){ //倒计时结束，关闭
                dispatch_source_cancel(_timer);
                dispatch_async(dispatch_get_main_queue(), ^{
                    
                    [checkBtn setTitle:@"免费获取短信验证码" forState:UIControlStateNormal];
                    checkBtn.userInteractionEnabled = YES;
                });
            }else{
                int seconds = timeout%60;
                NSString *strTime = [NSString stringWithFormat:@"%.2d", seconds];
                dispatch_async(dispatch_get_main_queue(), ^{
                    [checkBtn setTitle:[NSString stringWithFormat:@"%@秒后重新发送",strTime] forState:UIControlStateNormal];
                    checkBtn.userInteractionEnabled = NO;
                    
                });
                timeout--;
                
            }
        });
        dispatch_resume(_timer);
    }
}

-(void)showSimpleAlertView:(NSString*)message
{
    UIAlertView* alert = [[UIAlertView alloc] initWithTitle:@"提示信息"
                                                    message:message
                                                   delegate:nil
                                          cancelButtonTitle:@"确定"
                                          otherButtonTitles:nil];
    [alert show];
}




@end
